"""
Case Type   : 资源池化集群支持多链路
Case Name   : 备节点加入过程中断开全链路
Create At   : 2025/04/13
Owner       : @kyleze
Description :
    1.踢出备节点
    2.备机加入，并切断开全链路
    3.获取备机加入结果
    4.恢复链路，启动备机
    5.查看集群状态
Expect      :
    1.成功
    2.成功
    3.备机加入失败
    4.成功
    5.集群正常
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


class MultipathTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_user_node = Node('PrimaryDbUser')
        self.pri_root = Node(node="PrimaryRoot")
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.sta1_root = Node(node="Standby1Root")
        self.ip_1 = macro.STANDBY1_LUN_IP.split()[0].strip()
        self.ip_2 = macro.STANDBY1_LUN_IP.split()[1].strip()
        self.log.info("----获取备节点网络设备名称----")
        self.st1_network_card = []
        interfaces = self.sta1_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_1 in item or self.ip_2 in item:
                self.st1_network_card.append(item.split(' ')[1].split(':')[0])
        self.log.info(self.st1_network_card)

    def test_multipath(self):
        step = '--step1:踢出备节点 expect:成功--'
        self.log.info(step)
        stop_res = self.sta1_sh.exec_cm_ctl("stop", '-n 2')
        self.log.info(stop_res)
        self.assertIn(self.constant.cm_stop_node_success_msg, stop_res,
                      '执行失败' + step)

        step = '--step2:备机加入，并切断开全链路 expect:成功--'
        self.log.info(step)
        start_thread = \
            ComThread(self.sta1_sh.exec_cm_ctl,
                      args=('start', f'-n 2'))
        start_thread.setDaemon(True)
        start_thread.start()
        time.sleep(2)
        res = self.sta1_root.sh(f'ifdown {self.st1_network_card[0]};'
                                f'ifdown {self.st1_network_card[1]};').result()
        self.log.info(res)
        self.assertEqual(
            2, res.count("successfully deactivated"), "执行失败" + step)
        start_thread.join(120)

        step = '--step3:获取备机加入结果 expect:备机加入失败--'
        self.log.info(step)
        start_res = start_thread.get_result()
        self.log.info(start_res)
        start_bool = start_res is None or \
            self.constant.cm_start_node_success_msg not in start_res
        self.assertTrue(start_bool, "执行失败" + step)

        step = '--step4:恢复链路，查看集群状态 expect:集群正常--'
        self.log.info(step)
        res = self.sta1_root.sh(f'ifup {self.st1_network_card[0]}; '
                                f'ifup {self.st1_network_card[1]}').result()
        self.log.info(res)
        self.assertEqual(2, res.count("successfully activated"),
                         "执行失败" + step)
        start_res = self.primary_sh.exec_cm_ctl("start", '-n 2')
        self.log.info(start_res)
        self.assertIn(self.constant.cm_start_node_success_msg, start_res,
                      '执行失败' + step)

        step = '--step5:查看集群状态 expect:集群正常--'
        self.log.info(step)
        cluster_status = self.primary_sh.get_db_cluster_status('status')
        self.log.info(cluster_status)
        self.assertTrue(cluster_status, "执行失败" + step)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end----')

